04. 常用内置函数

本章导览:Python 内置函数

Python 的设计哲学之一是 ‘Batteries Included’(自带电池):

  • 内置函数无需导入任何模块即可直接使用
  • 在金融数据分析中可显著提高代码简洁性和执行效率
  • 本章将系统介绍最常用的内置函数及其商业数据分析应用

本章函数概览

函数类别 核心函数 典型应用场景
序列处理 len(), enumerate(), sum() 数据完整性检查、排名报告
极值函数 max(), min() 识别最佳/最差表现
排序函数 sorted() 股价排序、筛选高估低估
类型转换 int(), float(), str() 数据清洗、格式化
舍入函数 round() 金融报表精度控制
函数式工具 map(), filter(), zip() 批量计算、条件筛选

len():获取序列长度

len() 函数返回对象的长度或项目个数,适用于字符串、列表、元组、字典等。

  • 时间复杂度:列表、元组、字符串、字典均为 O(1)
  • 金融应用:检查数据完整性
Listing 1
# 示例数据: 2018年净利润排名前10位的证券公司
stock = ['中信证券', '国泰君安', '海通证券', '华泰证券', '广发证券',
         '招商证券', '申万宏源', '国信证券', '中信建投', '中国银河']

# len()函数返回列表的元素个数
num_companies = len(stock)
print(f'证券公司数量: {num_companies}')
证券公司数量: 10

len() 应用:数据完整性检查

Listing 2
# 检查数据是否完整
stock = ['中信证券', '国泰君安', '海通证券', '华泰证券', '广发证券',
         '招商证券', '申万宏源', '国信证券', '中信建投', '中国银河']

expected_count = 10  # 期望有10家公司

if len(stock) == expected_count:
    print('数据完整')
else:
    print(f'数据缺失, 期望{expected_count}家, 实际{len(stock)}家')
数据完整

核心要点:在分析数据前,先用 len() 验证数据量是否符合预期。

enumerate():枚举索引和值

enumerate() 在遍历序列时同时返回索引和值,常用于生成带序号的报告。

  • 语法enumerate(iterable, start=0)
  • start 参数指定起始序号(默认从 0 开始)
Listing 3
stock = ['中信证券', '国泰君安', '海通证券', '华泰证券', '广发证券',
         '招商证券', '申万宏源', '国信证券', '中信建投', '中国银河']

# start=1 表示排名从1开始
print('2018年净利润排名前10位的证券公司:')
for rank, company in enumerate(stock, start=1):
    print(f'  {rank:2d}. {company}')
2018年净利润排名前10位的证券公司:
   1. 中信证券
   2. 国泰君安
   3. 海通证券
   4. 华泰证券
   5. 广发证券
   6. 招商证券
   7. 申万宏源
   8. 国信证券
   9. 中信建投
  10. 中国银河

sum():求和计算

sum() 对可迭代对象进行求和,支持初始值参数。

  • 语法sum(iterable, start=0)
  • 金融应用:计算总利润、平均利润等统计量
Listing 4
profit = [98.76, 70.70, 57.71, 51.61, 46.32,
          44.46, 42.48, 34.31, 31.03, 29.32]

# 计算净利润总和
profit_total = sum(profit)
# 计算平均净利润
profit_average = profit_total / len(profit)

print(f'净利润总和: {profit_total:.2f} 亿元')
print(f'平均净利润: {profit_average:.4f} 亿元')
净利润总和: 506.70 亿元
平均净利润: 50.6700 亿元

⭐ 平台练习:常用内置函数综合应用

Listing 5
# ⚠️ 平台原始代码 - 请原样输入至教学平台(注释除外),平台才会判定答案正确
stock = ["中信证券","国泰君安","海通证券","华泰证券","广发证券","招商证券","申万宏源","国信证券","中信建设","中国银河"]  # 定义列表stock
# 定义列表profit
profit = [98.7643,70.7004,57.7071,51.6089,46.3205,44.4626,42.4781,34.3125,31.0343,29.3174]
# 定义列表return_Q1
return_Q1 = [0.547783,0.315274,0.594318,0.383333,0.275237,0.307463,0.356265,0.617682,1.93341,0.734604]
price = [24.78,20.15,14.03,22.41,16.17,17.52,5.52,13.54,25.55,11.83]  # 定义列表price
#输出列表中元素的个数
print(len(stock))   

#以列表方式输出stock
print(list(enumerate(stock,start=1)))  

#计算10家证券公司的净利润总和
profit_total =sum(profit)       

#计算每家证券公司平均净利润
profit_average =profit_total/len(stock)    

print("2018年净利润排名前10位的证券公司净利润总和(亿元)",profit_total)  # 输出2018年净利润排名前10位的证券公司净利润总和(
print("2018年净利润排名前10位的证券公司净利润平均数(亿元)",round(profit_average,4))  # 输出2018年净利润排名前10位的证券公司净利润平均数
#找出最大涨幅
return_max =max(return_Q1)    
#找出最小涨幅
return_min =min(return_Q1)                       

print("2019年1季度股价的最大涨幅",return_max)  # 输出2019年1季度股价的最大涨幅
print("2019年1季度股价的最小涨幅",return_min)  # 输出2019年1季度股价的最小涨幅
#将股价由小到大排序
price_sorted =sorted(price)                  
print(price_sorted)  # 输出按价格升序排列的证券公司股价列表
10
[(1, '中信证券'), (2, '国泰君安'), (3, '海通证券'), (4, '华泰证券'), (5, '广发证券'), (6, '招商证券'), (7, '申万宏源'), (8, '国信证券'), (9, '中信建设'), (10, '中国银河')]
2018年净利润排名前10位的证券公司净利润总和(亿元) 506.7061
2018年净利润排名前10位的证券公司净利润平均数(亿元) 50.6706
2019年1季度股价的最大涨幅 1.93341
2019年1季度股价的最小涨幅 0.275237
[5.52, 11.83, 13.54, 14.03, 16.17, 17.52, 20.15, 22.41, 24.78, 25.55]

max():找出最大值

max() 返回可迭代对象的最大值,用于识别最佳表现。

Listing 6
# 2019年1季度股价涨跌幅
stock = ['中信证券', '国泰君安', '海通证券', '华泰证券', '广发证券',
         '招商证券', '申万宏源', '国信证券', '中信建投', '中国银河']
return_Q1 = [0.547783, 0.315274, 0.594318, 0.383333, 0.275237,
             0.307463, 0.356265, 0.617682, 1.93341, 0.734604]

# 找出最大涨幅
return_max = max(return_Q1)
best_index = return_Q1.index(return_max)
best_company = stock[best_index]

print(f'最大涨幅: {return_max:.2%}')
print(f'表现最佳: {best_company}')
最大涨幅: 193.34%
表现最佳: 中信建投

min():找出最小值

min() 返回可迭代对象的最小值,用于识别风险资产。

Listing 7
return_Q1 = [0.547783, 0.315274, 0.594318, 0.383333, 0.275237,
             0.307463, 0.356265, 0.617682, 1.93341, 0.734604]

# 找出最小涨幅
return_min = min(return_Q1)
print(f'最小涨幅: {return_min:.2%}')

# 风险预警: 检测是否存在亏损
if return_min < 0:
    print(f'警告: 存在亏损, 最大跌幅 {return_min:.2%}')
else:
    print('所有股票本季度均为正收益')
最小涨幅: 27.52%
所有股票本季度均为正收益

max() / min()key 参数

key 参数可以自定义比较标准,在处理复杂数据结构时非常实用。

Listing 8
stocks_info = [
    {'name': '中信证券', 'profit': 98.76, 'pe': 15.2},
    {'name': '国泰君安', 'profit': 70.70, 'pe': 12.8},
    {'name': '海通证券', 'profit': 57.71, 'pe': 18.5}
]

# 按净利润找出最大
most_profitable = max(stocks_info, key=lambda x: x['profit'])
print(f'最盈利: {most_profitable["name"]}')

# 按PE比率找出最小(估值最低)
cheapest = min(stocks_info, key=lambda x: x['pe'])
print(f'最低PE: {cheapest["name"]}')
最盈利: 中信证券
最低PE: 国泰君安

sorted():排序操作

sorted() 返回排序后的新列表,原对象保持不变。

  • 语法sorted(iterable, key=None, reverse=False)
Listing 9
price = [24.78, 20.15, 14.03, 22.41, 16.17,
         17.52, 5.52, 13.54, 25.55, 11.83]

# 升序排序(默认)
price_sorted = sorted(price)
print(f'股价升序: {price_sorted}')

# 降序排序
price_desc = sorted(price, reverse=True)
print(f'股价降序: {price_desc}')
股价升序: [5.52, 11.83, 13.54, 14.03, 16.17, 17.52, 20.15, 22.41, 24.78, 25.55]
股价降序: [25.55, 24.78, 22.41, 20.15, 17.52, 16.17, 14.03, 13.54, 11.83, 5.52]

sorted() 应用:高估与低估筛选

Listing 10
price = [24.78, 20.15, 14.03, 22.41, 16.17,
         17.52, 5.52, 13.54, 25.55, 11.83]

# 假设公允价值为18元
fair_value = 18.0

# 列表推导式筛选
overvalued = [p for p in price if p > fair_value]
undervalued = [p for p in price if p < fair_value]

print(f'高估股票数量: {len(overvalued)}, 价格: {sorted(overvalued)}')
print(f'低估股票数量: {len(undervalued)}, 价格: {sorted(undervalued)}')
高估股票数量: 4, 价格: [20.15, 22.41, 24.78, 25.55]
低估股票数量: 6, 价格: [5.52, 11.83, 13.54, 14.03, 16.17, 17.52]

sorted().sort() 的区别

特性 sorted() .sort()
返回值 新列表 None(原地修改)
原对象 不变 改变
适用对象 所有可迭代对象 仅列表
Listing 11
prices = [24.78, 20.15, 14.03]

# sorted(): 创建新列表, 原列表不变
sorted_prices = sorted(prices)
print(f'原列表: {prices}')
print(f'新列表: {sorted_prices}')

# .sort(): 原地修改
prices_copy = prices.copy()
prices_copy.sort()
print(f'原地修改后: {prices_copy}')
原列表: [24.78, 20.15, 14.03]
新列表: [14.03, 20.15, 24.78]
原地修改后: [14.03, 20.15, 24.78]

类型转换:int(), float(), str()

这些函数在不同数据类型之间进行转换,在数据清洗中极为常用。

Listing 12
# 从CSV读取的数据通常是字符串类型
price_str = '1850.50'
volume_str = '2500000'

# 转换为数值类型后才能进行计算
price = float(price_str)
volume = int(volume_str)

# 计算成交额
turnover = price * volume
print(f'成交额: {turnover:,.0f} 元')
成交额: 4,626,250,000 元

类型转换应用:处理缺失值

Listing 13
# 包含缺失值的原始数据
missing_values = ['', 'N/A', 'None', '1850.50']

for val in missing_values:
    if val not in ['', 'N/A', 'None']:
        price = float(val)
        print(f'有效价格: {price}')
    else:
        print(f'缺失值: "{val}"')
缺失值: ""
缺失值: "N/A"
缺失值: "None"
有效价格: 1850.5

核心要点:先判断数据有效性,再执行类型转换,避免运行时错误。

round():数值舍入

  • 语法round(number, ndigits=None)
  • 注意:Python 3 采用银行家舍入(Round Half to Even)

\[ \large{ \text{round}(2.5) = 2, \quad \text{round}(3.5) = 4 } \]

Listing 14
profit = [98.76, 70.70, 57.71, 51.61, 46.32,
          44.46, 42.48, 34.31, 31.03, 29.32]
profit_total = sum(profit)
profit_average = profit_total / len(profit)

print(f'精确值:      {profit_average}')
print(f'保留4位小数: {round(profit_average, 4)}')
print(f'保留2位小数: {round(profit_average, 2)}')
print(f'取整:        {round(profit_average)}')
精确值:      50.67
保留4位小数: 50.67
保留2位小数: 50.67
取整:        51

银行家舍入 vs 常规四舍五入

Listing 15
# 银行家舍入: 选择最近的偶数
print(f'round(2.5) = {round(2.5)}')  # 输出: 2
print(f'round(3.5) = {round(3.5)}')  # 输出: 4

# 金融场景: 推荐使用 Decimal 模块
from decimal import Decimal, ROUND_HALF_UP

value = Decimal('2.5')
rounded = value.quantize(Decimal('1'), rounding=ROUND_HALF_UP)
print(f'常规四舍五入: {rounded}')  # 输出: 3
round(2.5) = 2
round(3.5) = 4
常规四舍五入: 3

金融建议:涉及资金计算时,使用 Decimal 模块确保精度。

statistics 模块:基本统计量

Python 标准库 statistics 模块提供了丰富的统计函数。

Listing 16
import statistics

returns = [0.05, 0.03, -0.02, 0.04, 0.06, 0.02, -0.01, 0.07]

mean_val = statistics.mean(returns)
median_val = statistics.median(returns)
stdev_val = statistics.stdev(returns)

print(f'算术平均: {mean_val:.4f}')
print(f'中位数:   {median_val:.4f}')
print(f'标准差:   {stdev_val:.4f}')
print(f'方差:     {statistics.variance(returns):.4f}')
算术平均: 0.0300
中位数:   0.0350
标准差:   0.0321
方差:     0.0010

金融应用:夏普比率计算

夏普比率衡量每承担一单位风险获得的超额收益:

\[ \large{ \text{Sharpe Ratio} = \frac{E(R) - R_f}{\sigma} } \]

Listing 17
import statistics

returns = [0.05, 0.03, -0.02, 0.04, 0.06, 0.02, -0.01, 0.07]
risk_free_rate = 0.02  # 无风险利率

excess_return = statistics.mean(returns) - risk_free_rate
sharpe_ratio = excess_return / statistics.stdev(returns)
print(f'夏普比率: {sharpe_ratio:.4f}')
夏普比率: 0.3118

map():映射函数

map() 将指定函数应用于可迭代对象的每个元素

  • 语法map(function, iterable, ...)
Listing 18
# 批量将字符串价格转换为浮点数
price_strings = ['24.78', '20.15', '14.03', '22.41', '16.17']
price_floats = list(map(float, price_strings))
print(f'转换结果: {price_floats}')
转换结果: [24.78, 20.15, 14.03, 22.41, 16.17]

map() 应用:批量计算涨跌幅

Listing 19
prices_old = [100, 105, 98, 102, 110]
prices_new = [105, 110, 95, 108, 115]

# 定义涨跌幅计算函数
calc_return = lambda old, new: (new - old) / old

# map 可接受多个可迭代对象
returns = list(map(calc_return, prices_old, prices_new))
print(f'涨跌幅: {[f"{r:.2%}" for r in returns]}')
涨跌幅: ['5.00%', '4.76%', '-3.06%', '5.88%', '4.55%']

提示map() 比显式 for 循环更简洁,适合对大量数据执行统一操作。

filter():过滤函数

filter() 根据指定函数的条件筛选元素

  • 语法filter(function, iterable)
Listing 20
all_returns = [0.05, -0.02, 0.03, -0.01, 0.04, -0.03, 0.06]

# 筛选正收益
positive_returns = list(filter(lambda x: x > 0, all_returns))
print(f'正收益: {[f"{r:.2%}" for r in positive_returns]}')

# 筛选高收益(>3%)
high_returns = list(filter(lambda x: x > 0.03, all_returns))
print(f'高收益: {[f"{r:.2%}" for r in high_returns]}')
正收益: ['5.00%', '3.00%', '4.00%', '6.00%']
高收益: ['5.00%', '4.00%', '6.00%']

filter() 应用:多条件筛选

Listing 21
stocks = [
    {'name': 'A', 'return': 0.05, 'risk': 0.10},
    {'name': 'B', 'return': 0.08, 'risk': 0.15},
    {'name': 'C', 'return': 0.06, 'risk': 0.08}
]

# 筛选条件: 收益 > 5% 且 风险 < 10%
filtered = list(filter(
    lambda s: s['return'] > 0.05 and s['risk'] < 0.10,
    stocks
))
print(f'符合条件的股票: {[s["name"] for s in filtered]}')
符合条件的股票: ['C']

zip():配对函数

zip() 将多个可迭代对象的元素逐一配对成元组。

Listing 22
names = ['中信证券', '国泰君安', '海通证券']
prices = [24.78, 20.15, 14.03]
volumes = [2500000, 1800000, 3200000]

print('股票数据:')
for name, price, volume in zip(names, prices, volumes):
    turnover = price * volume
    print(f'  {name}: 价格={price:.2f}, 成交量={volume:,}, 成交额={turnover:,.0f}')
股票数据:
  中信证券: 价格=24.78, 成交量=2,500,000, 成交额=61,950,000
  国泰君安: 价格=20.15, 成交量=1,800,000, 成交额=36,270,000
  海通证券: 价格=14.03, 成交量=3,200,000, 成交额=44,896,000

zip() 应用:创建字典与排序

Listing 23
names = ['中信证券', '国泰君安', '海通证券']
prices = [24.78, 20.15, 14.03]
volumes = [2500000, 1800000, 3200000]

# zip 快速创建字典
stock_dict = dict(zip(names, prices))
print(f'价格字典: {stock_dict}')

# 按成交量排序
print('\n按成交量排序:')
for name, price, volume in sorted(
    zip(names, prices, volumes), key=lambda x: x[2]
):
    print(f'  {name}: {volume:,}')
价格字典: {'中信证券': 24.78, '国泰君安': 20.15, '海通证券': 14.03}

按成交量排序:
  国泰君安: 1,800,000
  中信证券: 2,500,000
  海通证券: 3,200,000

综合案例:证券公司分析

Listing 24
companies = ['中信证券', '国泰君安', '海通证券', '华泰证券', '广发证券']
profits = [98.76, 70.70, 57.71, 51.61, 46.32]
prices = [24.78, 20.15, 14.03, 22.41, 16.17]

# 基本统计
print('=' * 50)
print('2018年净利润排名前5位证券公司分析')
print('=' * 50)
print(f'  净利润总额: {sum(profits):.2f} 亿元')
print(f'  平均净利润: {sum(profits)/len(companies):.2f} 亿元')
print(f'  最高净利润: {max(profits):.2f} 亿元')
print(f'  最低净利润: {min(profits):.2f} 亿元')
==================================================
2018年净利润排名前5位证券公司分析
==================================================
  净利润总额: 325.10 亿元
  平均净利润: 65.02 亿元
  最高净利润: 98.76 亿元
  最低净利润: 46.32 亿元

综合案例:排名与投资价值分析

Listing 25
companies = ['中信证券', '国泰君安', '海通证券', '华泰证券', '广发证券']
profits = [98.76, 70.70, 57.71, 51.61, 46.32]
prices = [24.78, 20.15, 14.03, 22.41, 16.17]

# 净利润排名(降序)
print('【净利润排名】')
for rank, (company, profit) in enumerate(
    sorted(zip(companies, profits), key=lambda x: x[1], reverse=True), 1
):
    print(f'  {rank}. {company}: {profit:.2f} 亿元')

# 投资价值分析: 净利润/股价比
ratios = list(zip(companies, [p / pr for p, pr in zip(profits, prices)]))
best = max(ratios, key=lambda x: x[1])
print(f'\n【净利润/股价比最高】{best[0]} ({best[1]:.2f})')
【净利润排名】
  1. 中信证券: 98.76 亿元
  2. 国泰君安: 70.70 亿元
  3. 海通证券: 57.71 亿元
  4. 华泰证券: 51.61 亿元
  5. 广发证券: 46.32 亿元

【净利润/股价比最高】海通证券 (4.11)

本章小结

函数 功能 关键用法
len() 获取长度 数据完整性检查
enumerate() 枚举索引+值 生成排名报告
sum() 求和 计算总利润/平均值
max() / min() 极值 识别最佳/最差表现
sorted() 排序 股价排序与筛选
round() 舍入 金融报表精度控制
map() / filter() 映射/过滤 批量计算与条件筛选
zip() 配对 多维数据组合